xquery version "1.0";

import module namespace request="http://exist-db.org/xquery/request";
import module namespace layout="http://www.example.com/layout" at "xmldb:exist:///db/projdb/modules/layout.xqm";

declare namespace pm = "http://www.politicalmashup.nl";
declare option exist:serialize "doctype-public=-//W3C//DTD&#160;XHTML&#160;1.1//EN
                                doctype-system=http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd";

declare variable $id {xs:string(request:get-parameter("id", ()))};
declare variable $individualTupel {doc('/db/projdb/participatie_parameters_ge2009.xml')/result/tupel[@ref=$id]};
declare variable $individualName {data($individualTupel/speaker)};
declare variable $subject {xs:string(request:get-parameter("label", ()))};

declare function local:getTopics( $name, $subject ) as element()*
{
    let $speaker := doc('/db/projdb/clusterdataSortedBySpeakers.xml')/speakers/speaker[data(name) = data($name)]
    let $topicIds := distinct-values( $speaker/clusters/cluster/label[data(name) = data($subject)]/topics/topic)
    (:let $sourceTopics := collection('/db/projdb/ge-2009')/root/pm:proceedings/pm:topic:)
    let $sourceTopics := doc('/db/projdb/topic_titles_for_subjects.xml')/topics/topic

    return 
        for $topicId at $position in $topicIds
            let $topic := $sourceTopics[data(@id) = data($topicId)]
            return  <li class="{if($position mod 2 = 0) then 'evenItem' else 'oddItem'}"> {
                <p><a href="topic.xql?id={$topicId}&amp;individualID={data($id)}">{data($topic/title)}</a></p>
            }</li>
    
};


<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="nl">
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
        <title>Overzicht agendapunten van {$individualName} omtrent het onderwerp {$subject}</title>
        <link rel="stylesheet" style="text/css" href="style/core_layout.css"/>
		<script type="text/javascript" src="js/jquery.js"><!-- --></script>
        <script type="text/javascript" src="https://www.google.com/jsapi"><!-- --></script>
        <script type="text/javascript" src="js/myScripts.js"><!-- --></script>
        <script type="text/javascript">
            var individualID = "{$id}";
            //<![CDATA[
            window.onload = function() {
                initShowHideButtons();
            }
            //]]>
        </script>
    </head>
    <body>	
         <div class="containerBox">
        {layout:topMenu()}

        <div class="box" id="titleBox">
            <div class="margin">
                <div id="individualInfoChartContainer">
                    <h1><a href="individual.xql?id={$id}">{$individualName}</a></h1>
                </div>
            </div>
        </div>

        <div class="box">
            <div class="margin">
                <h2 class="boxTitle">Agendapunten met onderwerp '{$subject}'</h2>
                <div class="boxContent">
                    <ul class="topicTitels">
                    {local:getTopics( $individualName, $subject )}
                    </ul>
                </div>
            </div>
        </div>
</div>
    </body>
</html>
